package service

import (
	"gin_gorm_oj/define"
	"gin_gorm_oj/help"
	"gin_gorm_oj/modles"
	"github.com/gin-gonic/gin"
	"log"
	"strconv"
)

// GetCategoryList
// @Summary 分类列表
// @Tags 公共方法
// @Param size query int false "size"
// @Param page query int false "page"
// @Param keyword query string false "keyword"
// @Success 200 {string} json "{"code":"200","msg","","data":""}"
// @Router /problem-list [get]
func GetCategoryList(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", define.DefaultPage))
	size, _ := strconv.Atoi(c.DefaultQuery("size", define.DefaultSize))
	page = (page - 1) * size
	var count int64
	keyword := c.Query("keyword")
	categoryList := make([]*modles.Category, 0)
	err := modles.DB.Model(new(modles.Category)).Where("name like ?", "%"+keyword+"%").Count(&count).
		Limit(size).Offset(page).Find(&categoryList).Error
	if err != nil {
		log.Println("分类err" + err.Error())
		c.JSON(200, gin.H{
			"code": -1,
			"msg":  "获取分类列表失败" + err.Error(),
		})
	}
	c.JSON(200, gin.H{
		"code":  200,
		"msg":   "分类列表获取成功",
		"data":  categoryList,
		"count": count,
	})
}

// CategoryCreate
// @Tags 管理员私有方法
// @Summary 创建分类
// @Param authorization header string true "authorization"
// @Param name formData string true "name"
// @Param parentId formData string true "parentId"
// @Success 200 {string} json "{"code":"200","data":""}"
// @Router /category-create [post]
func CategoryCreate(c *gin.Context) {
	name := c.PostForm("name")
	parentId, _ := strconv.Atoi(c.PostForm("parentId"))
	category := &modles.Category{
		Identity: help.GetUUID(),
		Name:     name,
		ParentId: parentId,
	}
	err := modles.DB.Create(category).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": -1,
			"msg":  "分类创建失败" + err.Error(),
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "创建成功",
	})
}

// CategoryDelete
// @Tags 管理员私有方法
// @Summary 删除分类
// @Param authorization header string true "authorization"
// @Param identity formData string true "identity"
// @Success 200 {string} json "{"code":"200","data":""}"
// @Router /category-delete [delete]
func CategoryDelete(c *gin.Context) {
	identity := c.PostForm("identity")
	if identity == "" {
		c.JSON(200, gin.H{
			"code": -1,
			"msg":  "参数为空",
		})
		return
	}
	var cnt int64
	err := modles.DB.Model(new(modles.ProblemCategory)).Where("category_id = (SELECT id FROM category WHERE identity ? LIMIT 1)", identity).Count(&cnt).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": -1,
			"msg":  "获取分类下问题错误" + err.Error(),
		})
		return
	}
	if cnt > 0 {
		c.JSON(200, gin.H{
			"code": -1,
			"msg":  "分类下有问题没删除",
		})
		return
	}
	err1 := modles.DB.Model(new(modles.Category)).Where("identity=?", identity).Delete(modles.Category{}).Error
	if err1 != nil {
		c.JSON(200, gin.H{
			"code": -1,
			"msg":  "删除分类出错" + err1.Error(),
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "删除成功",
	})
}

// CategoryUpdate
// @Tags 管理员私有方法
// @Summary 修改分类
// @Param authorization header string true "authorization"
// @Param identity formData string true "identity"
// @Param name formData string true "name"
// @Param parentId formData string true "parentId"
// @Success 200 {string} json "{"code":"200","data":""}"
// @Router /category-create [put]
func CategoryUpdate(c *gin.Context) {
	name := c.PostForm("name")
	parentId, _ := strconv.Atoi(c.PostForm("parentId"))
	identity := c.PostForm("identity")
	if name == "" || parentId == 0 || identity == "" {
		c.JSON(200, gin.H{
			"code": -1,
			"msg":  "参数不正确",
		})
		return
	}

	category := &modles.Category{
		Identity: identity,
		Name:     name,
		ParentId: parentId,
	}
	err := modles.DB.Model(new(modles.Category)).Where("identity=?", identity).Updates(category).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": -1,
			"msg":  "更新分类" + err.Error(),
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "修改成功",
	})

}
